package controller;


import org.springframework.web.bind.annotation.*;
import pojo.Page;
import pojo.SCMUser;
import response.Result;
import service.SCMUserService;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author Admin
 * @date 2022/11/17 17:48
 * @Description TODO
 * @Version 1.0
 **/
@RestController
public class SCMUserController {
    @Resource
    private SCMUserService service;

    @RequestMapping("getUsers")
    public Result getUsers(Integer currentPage, Integer pageSize, String account, String name){
        Page<SCMUser> p = new Page<SCMUser>();
        p.setCurrentPage(currentPage);
        p.setPageSize(pageSize);

        SCMUser scmUser = new SCMUser();
        scmUser.setAccount(account);
        scmUser.setName(name);

        int totalRecord = service.getCount(scmUser);

        List<SCMUser> userList = service.getUsers(p,scmUser);

        p.setLists(userList);
        p.setTotalRecord(totalRecord);

        return Result.ok(p);
    }

    @GetMapping("getUser")
    public Result getUser(String account){
        //验证用户
        SCMUser u = service.checkUser(account);
        return Result.ok(u);
    }
    //注册
    @PostMapping("register")
    public Result register(@RequestBody SCMUser u){
        service.addUser(u);
        return Result.ok();
    }
    //新增或修改
    @PostMapping("addOrUpdateUser")
    public Result addOrUpdateUser(@RequestBody SCMUser u){
        service.addOrUpdateUser(u);
        return Result.ok();
    }

    //删除
    @GetMapping("delUser")
    public Result delUser(String account){
        service.delUser(account);
        return Result.ok();
    }

    @GetMapping("getUserByAcc")
    public Result getUserByAcc(String account){
        SCMUser u = service.getUser(account);
        return Result.ok(u);
    }
}
